package co.yixiang.hw.service.impl;

import cn.hutool.core.date.DateUtil;
import co.yixiang.hw.domain.YxStoreCouponIssue;
import co.yixiang.hw.repository.YxStoreCouponIssueRepository;
import co.yixiang.hw.service.YxStoreCouponIssueService;
import co.yixiang.hw.service.dto.YxStoreCouponIssueDto;
import co.yixiang.hw.service.dto.YxStoreCouponIssueQueryCriteria;
import co.yixiang.hw.service.mapper.YxStoreCouponIssueMapper;
import co.yixiang.hw.vo.YxStoreCouponIssueVo;
import co.yixiang.utils.*;
import org.hibernate.query.internal.NativeQueryImpl;
import org.hibernate.query.spi.NativeQueryImplementor;
import org.hibernate.transform.Transformers;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

// 默认不使用缓存
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;


/**
* @author Walert
* @date 2020-03-10
*/
@Service
@CacheConfig(cacheNames = "yxStoreCouponIssue")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class YxStoreCouponIssueServiceImpl implements YxStoreCouponIssueService {

    @Resource
    private YxStoreCouponIssueRepository yxStoreCouponIssueRepository;
    @Resource
    private YxStoreCouponIssueMapper yxStoreCouponIssueMapper;
    @Resource
    private RedisUtils redisUtils;

//    @PersistenceContext
//    private EntityManager entityManager;


    @Override
    //@Cacheable
    public Map<String,Object> queryAll(YxStoreCouponIssueQueryCriteria criteria, Pageable pageable){
        Page<YxStoreCouponIssue> page = yxStoreCouponIssueRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(yxStoreCouponIssueMapper::toDto));
    }

    @Override
    //@Cacheable
    public List<YxStoreCouponIssueDto> queryAll(YxStoreCouponIssueQueryCriteria criteria){
        return yxStoreCouponIssueMapper.toDto(yxStoreCouponIssueRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    //@Cacheable(key = "#p0")
    public YxStoreCouponIssueDto findById(Integer id) {
        YxStoreCouponIssue yxStoreCouponIssue = yxStoreCouponIssueRepository.findById(id).orElseGet(YxStoreCouponIssue::new);
        ValidationUtil.isNull(yxStoreCouponIssue.getId(),"YxStoreCouponIssue","id",id);
        if(yxStoreCouponIssue.getRemainCount() > 0){
            Integer count = (Integer)redisUtils.hget("coupon::issue-" + yxStoreCouponIssue.getId(),"issue");
            if(count == null) {
                double hdecr = redisUtils.hdecr("coupon::issue-" + yxStoreCouponIssue.getId(), "issue", -yxStoreCouponIssue.getRemainCount());
                System.out.println("YxStoreCouponIssueDTO=====================>hdecr::" + hdecr);
            }
        }
        return yxStoreCouponIssueMapper.toDto(yxStoreCouponIssue);
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public YxStoreCouponIssueDto create(YxStoreCouponIssue resources) {
        return yxStoreCouponIssueMapper.toDto(yxStoreCouponIssueRepository.save(resources));
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void update(YxStoreCouponIssue resources) {
        YxStoreCouponIssue yxStoreCouponIssue = yxStoreCouponIssueRepository.findById(resources.getId()).orElseGet(YxStoreCouponIssue::new);
        ValidationUtil.isNull( yxStoreCouponIssue.getId(),"YxStoreCouponIssue","id",resources.getId());
        yxStoreCouponIssue.copy(resources);
        yxStoreCouponIssueRepository.save(yxStoreCouponIssue);
    }

    @Override
    //@CacheEvict(allEntries = true)
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            yxStoreCouponIssueRepository.deleteById(id);
        }
    }

    @Override
    public List<Map> getIssueCouponList(Integer uid, Integer goodsId){
        Long time  = DateUtil.parseDate(DateUtil.today()).getTime() /1000L;

        return yxStoreCouponIssueRepository.getIssueCouponList(uid,goodsId,time.intValue());
    }
}